<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class User extends Admin_Controller {
			
	public function __construct(){
        parent::__construct();
		$this->load->model('mod_user');
	}
	
 	public function index(){
		$this->profile();	
	}
   	   
   	public function profile(){
		$id = $this->session->userdata('userId');
		$data['data']	= $this->mod_user->getUserById($id);
		
		$data['title']	= 'User Profile';
		$data['page']	= 'user/user/profile';
		$data['action']	= 'Profile';
    	$this->load->view('layout/admin',$data);
   	}
   	   
   	public function edit(){
		$id = $this->session->userdata('userId');
		
		if($this->input->post('btnSave')){
        	$this->form_validation->set_rules('fullName', 'Full Name', 'trim|required|max_length[50]');
        	$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|max_length[150]');
        	$this->form_validation->set_rules('password', 'Password', 'trim|max_length[50]');
        	$this->form_validation->set_rules('conPassword', 'Confirm Password', 'trim|max_length[50]|matches[password]');
			$this->form_validation->set_error_delimiters('<div class="red">', '</div>');
			
        	if ($this->form_validation->run() == TRUE) {
				$fullName = $this->input->post('fullName');
				$email	  = $this->input->post('email');
				$password = $this->input->post('password');
				
				if($this->mod_user->update($id, $fullName, $email, $password)){
					$this->session->set_userdata('ms', 'Successfully changed!');
					if(!empty($password)) redirect('authentication/logout');
				} else {
					$this->session->set_userdata('ms', 'Could not updated, please try again!');
				}
				redirect('user/user/profile');
			}
		}
		$data['data']	= $this->mod_user->getUserById($id);
		
		$data['title']	= 'Edit User';
		$data['page']	= 'user/user/edit';
		$data['action']	= 'Edit Profile';
    	$this->load->view('layout/admin',$data);
   	}
}